package com.ambitor.ipro.actions;

import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.dao.ArticleDao;
import com.ambitor.ipro.util.SkyenccUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class ArticleAction extends BaseAction {

	private static final long serialVersionUID = -7019725516934646023L;

	private ArticleDao articleDAO;

	private IDataset notices;

	private IDataset faqs;

	private int displayNum;

	private Map<String, String> article;

	public ArticleAction() throws SQLException {
		super();
		articleDAO = new ArticleDao(pd.getConn());
	}

	@SuppressWarnings("unchecked")
	public String listNotices() throws Exception {
		Map<String, Object> application = ActionContext.getContext()
				.getApplication();
		IDataset notices_in_application = (IDataset) application
				.get("application.notices");
		if (notices_in_application != null) {
			this.setNotices(notices_in_application);
		} else {
			notices = articleDAO.queryArticlesByType("1");
			for (int i = 0; i < notices.size(); i++) {
				IData data = notices.getData(i);
				String updateTime = SkyenccUtil.format2YMD((Date) data
						.get("UPDATE_TIME"));
				data.put("UPDATE_TIME", updateTime);
			}
			if (notices != null)
				application.put("application.notices", notices);
		}
		return Action.SUCCESS;
	}

	public String listFaqs() throws Exception {
		Map<String, Object> application = ActionContext.getContext()
				.getApplication();
		IDataset faqs_in_application = (IDataset) application
				.get("application.faqs");
		if (faqs_in_application != null) {
			this.setFaqs(faqs_in_application);
		} else {
			faqs = articleDAO.queryArticlesByType("2");
			if (faqs != null)
				application.put("application.faqs", faqs);
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String getNoticeDetail() throws Exception {
		IData data = new DataMap();
		data.putAll(this.getArticle());
		article = articleDAO.queryArticleByPK(data);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String addNotice() throws Exception {
		IData data = new DataMap();
		data.putAll(this.getArticle());
		data.put("ARTICLE_TYPE", "1");
		data.put("UPDATE_TIME", SkyenccUtil.formatDate(new Date()));
		int result = articleDAO.insertArticle(data);
		if (result == 1) {
			Map<String, Object> application = ActionContext.getContext()
					.getApplication();
			notices = articleDAO.queryArticlesByType("1");
			application.put("application.notices", notices);
			faqs = articleDAO.queryArticlesByType("2");
			application.put("application.faqs", faqs);
			return SUCCESS;
		} else
			return INPUT;
	}

	@SuppressWarnings("unchecked")
	public IDataset getNotices() throws Exception {
		notices.sort("ARTICLE_ID", IDataset.TYPE_INTEGER,
				IDataset.ORDER_DESCEND);
		return notices;
	}

	public void setNotices(IDataset notices) {
		this.notices = notices;
	}

	public IDataset getFaqs() {
		faqs.sort("ARTICLE_ID", IDataset.TYPE_INTEGER, IDataset.ORDER_DESCEND);
		return faqs;
	}

	public void setFaqs(IDataset faqs) {
		this.faqs = faqs;
	}

	public void setDisplayNum(int displayNum) {
		this.displayNum = displayNum;
	}

	public int getDisplayNum() {
		if (displayNum <= 0)
			displayNum = 8;
		return displayNum;
	}

	public void setArticle(Map<String, String> article) {
		this.article = article;
	}

	public Map<String, String> getArticle() {
		return article;
	}
}
